package com.acertificate.domain;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class Moods {
	public static final String URL_PREFIX = "http://gexing.51gxqm.com/qianming/yulu/index_";
	public static final String SUFFIX = ".html";

	public static final String SQL_TEMPLATE = "INSERT INTO sync_mood(field1,field2) VALUES ('%s',5);";

	public static int maxProcess = 20;
	public static int total = 9;
	public static int threadCount;

	{
		threadCount = total / maxProcess + (total % maxProcess == 0 ? 0 : 1);
		System.out.println(threadCount);
	}

	public static void main(String[] args) throws ParserException, MalformedURLException, Exception {
		Moods wanmv = new Moods();
//		for (int i = 1; i <= threadCount; i++) {
//			RelolThread thread = wanmv.new RelolThread((i - 1) * maxProcess + 1, (i * maxProcess > total ? total : i * maxProcess));
//			thread.start();
//		}
		
		File file = new File("c:/mood");
		File [] fs =file.listFiles();
		StringBuffer sb = new StringBuffer();
		for (File file2 : fs) {
			sb.append(IOUtils.toString(new FileInputStream(file2)));
		}
		IOUtils.write(sb, new FileOutputStream(new File("c:/asdf.sql")));
		
	}

	class RelolThread extends Thread {

		public int start;
		public int end;

		public RelolThread() {
		}

		public RelolThread(int start, int end) {
			this.start = start;
			this.end = end;
		}

		public void run() {
			for (int i = start; i < end; i++) {
				getManifest(i, URL_PREFIX + i + SUFFIX);
			}
		}

		public List<String> getManifest(int x, String url) {
			System.out.println(url);
			List<String> results = new ArrayList<String>();
			try {
				URLConnection urlConnection = new URL(url).openConnection();
				String content1 = IOUtils.toString(urlConnection.getInputStream());
				Parser contentParser = new Parser();
				contentParser.setInputHTML(content1);

				NodeList xx = contentParser.parse(new HasAttributeFilter("class", "feedText"));
				for (int i = 0; i < xx.size(); i++) {
					Node node = xx.elementAt(i);
					String txt = node.toPlainTextString();
					if(txt.length() < 5)
						continue;
					txt = txt.trim().replace(" ", "");
					txt = txt.replace("\\", "\\ ");
					txt = txt.replace("\n", "");
					txt = txt.replace("\n\r", "");
//					txt = txt.replace("\"", "\"");
					txt = txt.replace("'", "\\'");
					results.add(txt);
//					System.out.println(txt);
				}

				StringBuffer sb = new StringBuffer();
				for (String string : results) {
					sb.append(String.format(SQL_TEMPLATE, new Object[] { string, System.currentTimeMillis() }));
					sb.append("\r\n");
				}
				try {
					String fileName = x + "-" + System.currentTimeMillis() + ".txt";
					System.out.println("generating SQL files " + fileName + " ...");
					IOUtils.write(sb.toString(), new FileOutputStream(new File("c:/mood/" + fileName)));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			return results;
		}
	}

	class Entity {
		private int id;
		private String en;

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getEn() {
			return en;
		}

		public void setEn(String en) {
			this.en = en;
		}

		public String getTime() {
			return time;
		}

		public void setTime(String time) {
			this.time = time;
		}

		private String time;
	}

}
